import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:osg/controllers/http.dart';

List<Map> items = [
  {'text': "全部", "index": 0},
  {'text': "待付款", "index": 1},
  {'text': "已完成", "index": 2},
  {'text': "已取消", "index": 3},
];
List<Map> items1 = [
  {'text': "全部", "index": 0},
  {'text': "寄售中", "index": 1},
  {'text': "已售出", "index": 2},
  {'text': "已取消", "index": 3},
];
List<Map> items2 = [
  {'text': "全部", "index": 0},
  {'text': "委托中", "index": 2},
  {'text': "已完成", "index": 3},
];
List<Map> items3 = [
  {'text': "全部", "index": 0},
  {'text': "竞价中", "index": 2},
  {'text': "已完成", "index": 3},
];

RxInt _index = 0.obs;
RxInt index = 0.obs;
RxList listData = [].obs;

getOrderList(url,status) {
    // 订单
    Get.find<HTTPS>().getData(url, {
      "status": status,
      "per_page": '999'
    }).then((res) {
      if (res != null) {
        listData.value = res['list'];
      }
    });
  }

class OrderList extends StatefulWidget {
  const OrderList({super.key});
  @override
  State<OrderList> createState() => _HomePageState();
}

class _HomePageState extends State<OrderList>
    with SingleTickerProviderStateMixin {
  late TabController _tabController;

  

  @override
  void initState() {
    super.initState();
    _tabController = TabController(length: 4, vsync: this);
    WidgetsBinding.instance.addPostFrameCallback((_) {
      print('object');
      getOrderList('/order/list','all');
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          backgroundColor: Color(0xffffffff),
          title: Text('藏品订单'),
          centerTitle: true,
          bottom: TabBar(
              onTap: (vlaue) => {
                    _index.value = 0,
                    index.value = vlaue,
                    listData.value=[],
                    if (vlaue == 0) {
                      getOrderList('/order/list','all')
                    }else if(vlaue == 1){
                      getOrderList('/resale/my_list','all')

                    }else if(vlaue == 2){
                      getOrderList('/want/orders','all')

                    }else{
                      getOrderList('/bidding/orders','all')

                    }
                  },
              indicatorColor: Color(0XFF0F172B),
              indicatorWeight: 3.0,
              labelPadding: EdgeInsets.symmetric(vertical: 10),
              indicatorSize: TabBarIndicatorSize.tab,
              labelColor: Color(0XFF0F172B),
              unselectedLabelColor: Color(0xff999999),
              dividerColor: Color(0XFFF8F9FD),
              dividerHeight: 4,
              controller: _tabController,
              overlayColor: WidgetStateProperty.all(Colors.transparent),
              tabs: [
                Text(
                  '我的订单',
                  style: TextStyle(fontSize: 14),
                ),
                Text(
                  '我的寄售',
                  style: TextStyle(fontSize: 14),
                ),
                Text(
                  '我的委托',
                  style: TextStyle(fontSize: 14),
                ),
                Text(
                  '我的竞价',
                  style: TextStyle(fontSize: 14),
                ),
              ]),
        ),
        body: TabBarView(controller: _tabController, children: [
          Obx(
            () => ListView(
              padding: EdgeInsets.only(top: 20),
              children: [
                titleList(data: items),
                for (var item in listData)
                  GestureDetector(
                    onTap: () =>
                        {Get.toNamed('/web_tabs/order/consignment_detail')},
                    child: Container(
                      margin:
                          EdgeInsets.symmetric(horizontal: 15, vertical: 15),
                      padding:
                          EdgeInsets.symmetric(horizontal: 15, vertical: 10),
                      decoration: BoxDecoration(
                          color: Color(0XFFF8F9FD),
                          borderRadius: BorderRadius.circular(15)),
                      child: Column(
                        children: [
                          Row(
                            mainAxisAlignment: MainAxisAlignment.spaceBetween,
                            children: [
                              Text('订单编号:${item['order_money_price']}',
                                  style: TextStyle(
                                      fontSize: 12, color: Color(0XFF0F172B))),
                              Text('${item['union_status_text']}',
                                  style: TextStyle(
                                      fontSize: 12, color: Color(0XFF0F172B))),
                            ],
                          ),
                          SizedBox(
                            height: 10,
                          ),
                          Row(
                            mainAxisAlignment: MainAxisAlignment.spaceBetween,
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: [
                              ClipRRect(
                                borderRadius:
                                    BorderRadius.circular(10.0), // 设置圆角大小
                                child: Image.network('${item['thumb']}',
                                    width: 80, height: 80, fit: BoxFit.fill,
                                    errorBuilder: (context, error, stackTrace) {
                                  return SizedBox(
                                    width: 80,
                                  );
                                }),
                              ),
                              SizedBox(
                                width: 10,
                              ),
                              Column(
                                crossAxisAlignment: CrossAxisAlignment.start,
                                children: [
                                  Text('${item['title']}',
                                      style: TextStyle(
                                          fontSize: 14,
                                          color: Color(0XFF0F172B))),
                                  SizedBox(
                                    height: 35,
                                  ),
                                  Row(
                                    children: [
                                      Text('￥',
                                          style: TextStyle(
                                              fontSize: 11,
                                              color: Color(0XFF0F172B))),
                                      Text('${item['order_money_price'] / 100}',
                                          style: TextStyle(
                                              fontSize: 16,
                                              color: Color(0XFF0F172B),
                                              fontWeight: FontWeight.bold)),
                                    ],
                                  )
                                ],
                              ),
                              Expanded(child: SizedBox()),
                              Container(
                                margin: EdgeInsets.only(top: 5),
                                child: Text('x${item['total']}',
                                    style: TextStyle(
                                        fontSize: 12,
                                        color: Color(0XFF0F172B))),
                              )
                            ],
                          ),
                          SizedBox(
                            height: 10,
                          ),
                          Row(
                            mainAxisAlignment: MainAxisAlignment.spaceBetween,
                            children: [
                              Text('创建时间',
                                  style: TextStyle(
                                      fontSize: 12, color: Color(0XFF999999))),
                              Text('${item['created_at']}',
                                  style: TextStyle(
                                      fontSize: 12, color: Color(0XFF0F172B))),
                            ],
                          ),
                         
                          if(item['status']==1&&index.value!=1)
                          Container(
                            padding: EdgeInsets.only(top: 10),
                            margin: EdgeInsets.only(top: 10),
                            decoration: BoxDecoration(
                              border: Border(top: BorderSide(color: Color(0XFFDDDDDD)))
                            ),
                            child:  Row(
                            mainAxisAlignment: MainAxisAlignment.end,
                            children: [
                              Text('取消',
                                  style: TextStyle(
                                      fontSize: 12, color: Color(0XFF0F172B))),
                                          SizedBox(
                                width: 30,
                              ),
                              Container(
                                  height: 25,
                                  width: 50,
                                  alignment: Alignment.center,
                                  decoration: BoxDecoration(
                                      color: Color(0XFF0F172B),
                                      borderRadius: BorderRadius.circular(25)),
                                  child: GestureDetector(
                                    onTap: () => {
                                      Get.toNamed(
                                          '/web_tabs/order/pending_payment')
                                    },
                                    child: Text('付款',
                                        style: TextStyle(
                                            fontSize: 12,
                                            color: Color(0XFFFFFFFF))),
                                  ))
                            ],
                          ),
                          ),

                          // Container(
                          //   padding: EdgeInsets.only(top: 15),
                          //   margin: EdgeInsets.only(top: 15),
                          //   decoration: BoxDecoration(
                          //     border: Border(top: BorderSide(color: Color(0XFFDDDDDD)))
                          //   ),
                          //   child:  Row(
                          //   mainAxisAlignment: MainAxisAlignment.end,
                          //   children: [
                          //     Text('取消',
                          //         style: TextStyle(
                          //             fontSize: 12, color: Color(0XFF0F172B))),
                          //     SizedBox(
                          //       width: 30,
                          //     ),
                          //     Container(
                          //         height: 25,
                          //         width: 50,
                          //         alignment: Alignment.center,
                          //         decoration: BoxDecoration(
                          //             color: Color(0XFF0F172B),
                          //             borderRadius: BorderRadius.circular(25)),
                          //         child: GestureDetector(
                          //           onTap: () => {
                          //             Get.toNamed(
                          //                 '/web_tabs/order/pending_payment')
                          //           },
                          //           child: Text('付款',
                          //               style: TextStyle(
                          //                   fontSize: 12,
                          //                   color: Color(0XFFFFFFFF))),
                          //         ))
                          //   ],
                          // ),
                          // )
                         
                         
                        ],
                      ),
                    ),
                  )
              ],
            ),
          ),
          ListView(
            padding: EdgeInsets.only(top: 20),
            children: [
              titleList(data: items1),
              Container(
                margin: EdgeInsets.symmetric(horizontal: 15, vertical: 15),
                padding: EdgeInsets.symmetric(horizontal: 15, vertical: 10),
                decoration: BoxDecoration(
                    color: Color(0XFFF8F9FD),
                    borderRadius: BorderRadius.circular(15)),
                child: Column(
                  children: [
                    Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        Text('订单编号:234239482095234',
                            style: TextStyle(
                                fontSize: 12, color: Color(0XFF0F172B))),
                        Text('已售出',
                            style: TextStyle(
                                fontSize: 12, color: Color(0XFF0F172B))),
                      ],
                    ),
                    SizedBox(
                      height: 10,
                    ),
                    Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        ClipRRect(
                          borderRadius: BorderRadius.circular(10.0), // 设置圆角大小
                          child: Image.asset(
                            'static/aaa.png',
                            width: 80,
                            height: 80,
                            fit: BoxFit.fill,
                          ),
                        ),
                        SizedBox(
                          width: 10,
                        ),
                        Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: [
                            Text('超现实元宇宙数字艺术展厅',
                                style: TextStyle(
                                    fontSize: 14, color: Color(0XFF0F172B))),
                            SizedBox(
                              height: 35,
                            ),
                            Row(
                              children: [
                                Text('￥',
                                    style: TextStyle(
                                        fontSize: 11,
                                        color: Color(0XFF0F172B))),
                                Text('99.00',
                                    style: TextStyle(
                                        fontSize: 16,
                                        color: Color(0XFF0F172B),
                                        fontWeight: FontWeight.bold)),
                              ],
                            )
                          ],
                        ),
                        Expanded(child: SizedBox()),
                        Container(
                          margin: EdgeInsets.only(top: 5),
                          child: Text('x23',
                              style: TextStyle(
                                  fontSize: 12, color: Color(0XFF0F172B))),
                        )
                      ],
                    ),
                    SizedBox(
                      height: 10,
                    ),
                    Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        Text('创建时间',
                            style: TextStyle(
                                fontSize: 12, color: Color(0XFF999999))),
                        Text('2020-12-16 16:23:23',
                            style: TextStyle(
                                fontSize: 12, color: Color(0XFF0F172B))),
                      ],
                    ),
                    Divider(
                      color: Color(0XFFDDDDDD),
                    ),
                    Row(
                      mainAxisAlignment: MainAxisAlignment.end,
                      children: [
                        Text('取消寄售',
                            style: TextStyle(
                                fontSize: 12, color: Color(0XFF0F172B))),
                      ],
                    )
                  ],
                ),
              )
            ],
          ),
          ListView(
            padding: EdgeInsets.only(top: 20),
            children: [
              titleList(data: items2),
              Container(
                margin: EdgeInsets.symmetric(horizontal: 15, vertical: 15),
                padding: EdgeInsets.symmetric(horizontal: 15, vertical: 10),
                decoration: BoxDecoration(
                    color: Color(0XFFF8F9FD),
                    borderRadius: BorderRadius.circular(15)),
                child: Column(
                  children: [
                    Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        Text('订单编号:234239482095234',
                            style: TextStyle(
                                fontSize: 12, color: Color(0XFF0F172B))),
                        Text('委托中',
                            style: TextStyle(
                                fontSize: 12, color: Color(0XFF542AFF))),
                      ],
                    ),
                    SizedBox(
                      height: 10,
                    ),
                    Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        ClipRRect(
                          borderRadius: BorderRadius.circular(10.0), // 设置圆角大小
                          child: Image.asset(
                            'static/aaa.png',
                            width: 80,
                            height: 80,
                            fit: BoxFit.fill,
                          ),
                        ),
                        SizedBox(
                          width: 10,
                        ),
                        Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: [
                            Text('超现实元宇宙数字艺术展厅',
                                style: TextStyle(
                                    fontSize: 14, color: Color(0XFF0F172B))),
                            SizedBox(
                              height: 35,
                            ),
                            Row(
                              children: [
                                Text('￥',
                                    style: TextStyle(
                                        fontSize: 11,
                                        color: Color(0XFF0F172B))),
                                Text('99.00',
                                    style: TextStyle(
                                        fontSize: 16,
                                        color: Color(0XFF0F172B),
                                        fontWeight: FontWeight.bold)),
                              ],
                            )
                          ],
                        ),
                        Expanded(child: SizedBox()),
                        Container(
                          margin: EdgeInsets.only(top: 5),
                          child: Text('x23',
                              style: TextStyle(
                                  fontSize: 12, color: Color(0XFF0F172B))),
                        )
                      ],
                    ),
                    SizedBox(
                      height: 10,
                    ),
                    Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        Text('已购10个剩余，￥100已原路退换',
                            style: TextStyle(
                                fontSize: 12, color: Color(0XFF999999))),
                        Container(
                            width: 50,
                            alignment: Alignment.center,
                            height: 25,
                            decoration: BoxDecoration(
                                color: Color(0XFF0F172B),
                                borderRadius: BorderRadius.circular(25)),
                            child: GestureDetector(
                              onTap: () => {
                                Get.toNamed('/web_tabs/order/purchase_details')
                              },
                              child: Text('明细',
                                  style: TextStyle(
                                      fontSize: 12, color: Color(0XFFFFFFFF))),
                            ))
                      ],
                    ),
                    Divider(
                      color: Color(0XFFDDDDDD),
                    ),
                    Row(
                      mainAxisAlignment: MainAxisAlignment.end,
                      children: [
                        Text('取消寄售',
                            style: TextStyle(
                                fontSize: 12, color: Color(0XFF0F172B))),
                      ],
                    )
                  ],
                ),
              )
            ],
          ),
          Obx(() => ListView(
                padding: EdgeInsets.only(top: 20),
                children: [
                  titleList(data: items3),
                  for (var item in listData)
                    Container(
                      margin:
                          EdgeInsets.symmetric(horizontal: 15, vertical: 15),
                      padding:
                          EdgeInsets.symmetric(horizontal: 15, vertical: 10),
                      decoration: BoxDecoration(
                          color: Color(0XFFF8F9FD),
                          borderRadius: BorderRadius.circular(15)),
                      child: Column(
                        children: [
                          Row(
                            mainAxisAlignment: MainAxisAlignment.spaceBetween,
                            children: [
                              Text('订单编号:${item['number']}',
                                  style: TextStyle(
                                      fontSize: 12, color: Color(0XFF0F172B))),
                              Text('${item['union_status_text']}',
                                  style: TextStyle(
                                      fontSize: 12, color: Color(0XFF0F172B))),
                            ],
                          ),
                          SizedBox(
                            height: 10,
                          ),
                          Row(
                            mainAxisAlignment: MainAxisAlignment.spaceBetween,
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: [
                              ClipRRect(
                                borderRadius:
                                    BorderRadius.circular(10.0), // 设置圆角大小
                                child: Image.network('${item['thumb']}',
                                    width: 80, height: 80, fit: BoxFit.fill,
                                    errorBuilder: (context, error, stackTrace) {
                                  return SizedBox(
                                    width: 80,
                                  );
                                }),
                              ),
                              SizedBox(
                                width: 10,
                              ),
                              Column(
                                crossAxisAlignment: CrossAxisAlignment.start,
                                children: [
                                  Text('${item['title']}',
                                      style: TextStyle(
                                          fontSize: 14,
                                          color: Color(0XFF0F172B))),
                                  SizedBox(
                                    height: 35,
                                  ),
                                  Row(
                                    children: [
                                      Text('￥',
                                          style: TextStyle(
                                              fontSize: 11,
                                              color: Color(0XFF0F172B))),
                                      Text('${item['order_money_price']}/100',
                                          style: TextStyle(
                                              fontSize: 16,
                                              color: Color(0XFF0F172B),
                                              fontWeight: FontWeight.bold)),
                                    ],
                                  )
                                ],
                              ),
                              Expanded(child: SizedBox()),
                              Container(
                                margin: EdgeInsets.only(top: 5),
                                child: Text('x23',
                                    style: TextStyle(
                                        fontSize: 12,
                                        color: Color(0XFF0F172B))),
                              )
                            ],
                          ),
                          SizedBox(
                            height: 10,
                          ),
                          Row(
                            mainAxisAlignment: MainAxisAlignment.spaceBetween,
                            children: [
                              Text('创建时间',
                                  style: TextStyle(
                                      fontSize: 12, color: Color(0XFF999999))),
                              Text('2020-12-16 16:23:23',
                                  style: TextStyle(
                                      fontSize: 12, color: Color(0XFF0F172B))),
                            ],
                          ),
                          Divider(
                            color: Color(0XFFDDDDDD),
                          ),
                          Row(
                            mainAxisAlignment: MainAxisAlignment.end,
                            children: [
                              Text('取消',
                                  style: TextStyle(
                                      fontSize: 12, color: Color(0XFF0F172B))),
                              SizedBox(
                                width: 30,
                              ),
                              Container(
                                width: 50,
                                alignment: Alignment.center,
                                height: 25,
                                decoration: BoxDecoration(
                                    color: Color(0XFF0F172B),
                                    borderRadius: BorderRadius.circular(25)),
                                child: Text('付款',
                                    style: TextStyle(
                                        fontSize: 12,
                                        color: Color(0XFFFFFFFF))),
                              )
                            ],
                          )
                        ],
                      ),
                    )
                ],
              )),
        ]));
  }
}

class titleList extends StatelessWidget {
  final List<Map> data;

  const titleList({super.key, required this.data});

  @override
  Widget build(BuildContext context) {
    return Obx(() => Row(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: data
              .map(
                (item) => GestureDetector(
                  onTap: () {
                    _index.value = item['index'];
                    if(index.value==0){
                        if(item['index']==0){
                          getOrderList('/order/list','all');
                        }else if(item['index']==1){
                          getOrderList('/order/list','pay_pending');
                        }else if(item['index']==2){
                          getOrderList('/order/list','pay_ok');
                        }else if(item['index']==3){
                          getOrderList('/order/list','close');
                        }
                    }else if(index.value==1){

                      if(item['index']==0){
                                                  getOrderList('/resale/my_list','all');

                        }else if(item['index']==1){
                                                  getOrderList('/resale/my_list','pending');

                        }else if(item['index']==2){
                                                  getOrderList('/resale/my_list','completed');

                        }else if(item['index']==3){
                                                  getOrderList('/resale/my_list','closed');

                        }

                    }else if(index.value==2){
                        if(item['index']==0){
                                                   getOrderList('/want/orders','all');

                        }else if(item['index']==1){
                                                   getOrderList('/want/orders','wait_want');

                        }else if(item['index']==2){
                                                 getOrderList('/want/orders','deal');

                        }
                    }else if(index.value==3){
                        if(item['index']==0){
                                                  getOrderList('/bidding/orders','all');

                        }else if(item['index']==1){
                                                  getOrderList('/bidding/orders','wait_bidding');

                        }else if(item['index']==2){
                                                getOrderList('/bidding/orders','deal');

                        }
                    }
                    
                  },
                  child: Container(
                    margin: EdgeInsets.only(left: 15),
                    height: 28,
                    alignment: Alignment.center,
                    decoration: BoxDecoration(
                        color: _index.value == item['index']
                            ? Color(0XFF0F172B)
                            : Color(0XFFF8F9FD),
                        borderRadius: BorderRadius.circular(28)),
                    padding: EdgeInsets.symmetric(horizontal: 15),
                    child: Text(item['text'],
                        style: TextStyle(
                            fontSize: 12,
                            color: _index.value == item['index']
                                ? Color(0XFFffffff)
                                : Color(0XFF0F172B))),
                  ),
                ),
              )
              .toList(),
        ));
  }
}
